/********************************************************************************
 * BannerAdvertisersAdminAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Dec 2, 2008 4:47:53 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.BannerDailyStatistics;
import com.printeverywhere.dto.BannerHourlyStatistics;
import com.printeverywhere.dto.BannerSettings;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.SearchCriteria;

/*
 * Campaign status values
 *
 * 1. Pending
 * 2. Approved
 * 3. Rejected
 * 4. Suspended
 *
 */

@SuppressWarnings("unchecked")
public class BannerAdvertisersAdminAction extends AdminEngineManagement implements ServletRequestAware {

	/** */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private List<BannerCampaigns> banners = new ArrayList<BannerCampaigns>();

	private List<BannerSettings> bannerType = (List<BannerSettings>) CommonFunctionDelegator.getAllfromtable(BannerSettings.class.getName());

	private Integer bannerTypeId;

	private Integer status;

	private String userId;

	private int pageSize = 0;

	private GeneralCategories generalCategories = new GeneralCategories();

	private List<GeneralCategories> tempGeneralCategoriesList;

	private List<Integer> breadCrumbIdList = new ArrayList<Integer>();

	private List<GeneralCategories> generalCategoriesNamesList = new ArrayList<GeneralCategories>();
	
	private List statisticsList;
	
	private int bannerReportType;
	
	private String statusType;

	public String getStatusType() {
		return statusType;
	}

	public void setStatusType(String statusType) {
		this.statusType = statusType;
	}

	public int getBannerReportType() {
		return bannerReportType;
	}

	public void setBannerReportType(int bannerReportType) {
		this.bannerReportType = bannerReportType;
	}

	public List getStatisticsList() {
		return statisticsList;
	}

	public void setStatisticsList(List statisticsList) {
		this.statisticsList = statisticsList;
	}

	public List<GeneralCategories> getGeneralCategoriesNamesList() {
		return generalCategoriesNamesList;
	}

	public void setGeneralCategoriesNamesList(
			List<GeneralCategories> generalCategoriesNamesList) {
		this.generalCategoriesNamesList = generalCategoriesNamesList;
	}

	public List<Integer> getBreadCrumbIdList() {
		return breadCrumbIdList;
	}

	public void setBreadCrumbIdList(List<Integer> breadCrumbIdList) {
		this.breadCrumbIdList = breadCrumbIdList;
	}

	public List<GeneralCategories> getTempGeneralCategoriesList() {
		return tempGeneralCategoriesList;
	}

	public void setTempGeneralCategoriesList(
			List<GeneralCategories> tempGeneralCategoriesList) {
		this.tempGeneralCategoriesList = tempGeneralCategoriesList;
	}

	public GeneralCategories getGeneralCategories() {
		return generalCategories;
	}

	public void setGeneralCategories(GeneralCategories generalCategories) {
		this.generalCategories = generalCategories;
	}

	public void setCategoryForCampaigns() throws Exception{
		try{
			for (int i = 0; i < banners.size(); i++) {
				if(banners.get(i).getGeneralCategories() != null){
					generalCategories = getGeneralCategoriesFromCategoryId(banners.get(i).getGeneralCategories().getCategoryId());
					GeneralCategories mainParentGeneralCategories = new GeneralCategories();
					if(generalCategories.getMainParentId() != null){
						mainParentGeneralCategories = getGeneralCategoriesFromCategoryId(generalCategories.getMainParentId());
					}else{
						mainParentGeneralCategories = getGeneralCategoriesFromCategoryId(generalCategories.getCategoryId());
					}
					breadCrumbIdList = CategoryManagerServiceDelegator
					.getBreadCrumbList( generalCategories, mainParentGeneralCategories);
					String categoryNames[] = new String[breadCrumbIdList.size()] ;
					int index = 0;
					for (Iterator iterator = breadCrumbIdList.iterator(); iterator.hasNext();) {
						int id = Integer.parseInt(iterator.next().toString());
						 GeneralCategories generalCategories = getGeneralCategoriesFromCategoryId(id);
						 if(index == breadCrumbIdList.size()-1){
							 categoryNames[index] = generalCategories.getCategoryName();
						 }else{
							 categoryNames[index] = generalCategories.getCategoryName() + "->";
						 }
						 index++;
					}
					banners.get(i).setCategories(categoryNames);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getAllfromtable(BannerCampaigns.class.getName());
		setCategoryForCampaigns();
		return SUCCESS;
	}

	public GeneralCategories getGeneralCategoriesFromCategoryId(int categoryid){
		 try{
		 tempGeneralCategoriesList = (List<GeneralCategories>) ActionContext.getContext().getApplication().get(AdminConstants.GENERALCATEGORIES);
			GeneralCategories type = new GeneralCategories();
			for (Iterator<GeneralCategories> iterator = tempGeneralCategoriesList.iterator(); iterator.hasNext();) {
				type = (GeneralCategories) iterator.next();
				if(categoryid == type.getCategoryId()){
					return type;
				}
			}
		 }catch (Exception e) {
			 addActionError(e.getMessage());
		}
			return null;
	 }

	public String showCampaigns() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("status", 1);
		this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);
		setCategoryForCampaigns();
		return SUCCESS;
	}

	public String findManageCampaigns() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		SearchCriteria searchCriteria = new SearchCriteria();
		if(userId != null && userId.trim().length() > 0) {
			User bannerUser = UserServiceDelegator.getUserByUserId(userId);
			searchCriteria.setAttribute("user", bannerUser);
		}
		if(status != null && status.intValue() > 0)
			searchCriteria.setAttribute("status", this.status.intValue());
		if(bannerTypeId != null && bannerTypeId.intValue() > 0)
			searchCriteria.setAttribute("bannerSettings.id", this.bannerTypeId.intValue());

		if(request.getParameter(AdminConstants.CATEGORYID) != null){
			searchCriteria.setAttribute("generalCategories.categoryId", Integer.parseInt(request.getParameter(AdminConstants.CATEGORYID)));
		}

		this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);
		setCategoryForCampaigns();
		return SUCCESS;
	}

	public String approveCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);

				if(this.banners.size() > 0) {
					this.banners.get(0).setStatus(5);
					CommonFunctionDelegator.generalUpdatebyobj(this.banners.get(0));
					addActionMessage("Selected banner approved Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while approved selected banner");
		}
		return showCampaigns();
	}

	public String rejectCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);

				if(this.banners.size() > 0) {
					this.banners.get(0).setStatus(3);
					CommonFunctionDelegator.generalUpdatebyobj(this.banners.get(0));
					addActionMessage("Selected banner rejected Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while reject selected banner");
		}
		return showCampaigns();
	}

	public String deleteCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);
				if(this.banners != null && this.banners.size() > 0){
					CommonFunctionDelegator.GeneralDelete(this.banners.get(0));
					addActionMessage("Selected banner deleted Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while delete selected banner");
		}
		return showCampaigns();
	}


	public String suspendManageCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);

				if(this.banners.size() > 0) {
					this.banners.get(0).setStatus(4);
					CommonFunctionDelegator.generalUpdatebyobj(this.banners.get(0));
					addActionMessage("Selected banner suspend Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while suspending selected banner");
		}
		return findManageCampaigns();
	}

	public String approveManageCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);

				if(this.banners.size() > 0) {
					this.banners.get(0).setStatus(2);
					CommonFunctionDelegator.generalUpdatebyobj(this.banners.get(0));
					addActionMessage("Selected banner active Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while active selected banner");
		} 
		
		return findManageCampaigns();
	}

	public String deleteManageCampaigns() throws Exception {
		try{
			if(request.getParameter("id") != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("id", Integer.parseInt(request.getParameter("id")));
				this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);
				if(this.banners != null && this.banners.size() > 0){
					CommonFunctionDelegator.GeneralDelete(this.banners.get(0));
					addActionMessage("Selected banner Delete Successfully");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while deleting selected banner");
		}
		
		return findManageCampaigns();
	}
	
	public String showCampaignsStatus() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getAllfromtable(BannerCampaigns.class.getName());
		
		bannerTypeId = Integer.parseInt(request.getParameter("id"));
		
		if(bannerTypeId != null){
			SearchCriteria criteria = new SearchCriteria();
			criteria.removeAllAttribute();
			criteria.setAttribute("bannerCampaigns.id", bannerTypeId);
			statisticsList = CommonFunctionDelegator.GeneralgetCriteriaList(BannerDailyStatistics.class.getName(), criteria);
		}

		return SUCCESS;
	}
	
	public String getBannerStatistics() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		SearchCriteria criteria = new SearchCriteria();
		criteria.removeAllAttribute();

		try{
			if(bannerTypeId != null){
				criteria.setAttribute("bannerCampaigns.id", bannerTypeId);
			}
			
			if(bannerReportType == 1){
				statisticsList = CommonFunctionDelegator.GeneralgetCriteriaList(BannerDailyStatistics.class.getName(), criteria);
				statusType = "daily";
			}else if(bannerReportType == 2){
				statisticsList = CommonFunctionDelegator.GeneralgetCriteriaList(BannerHourlyStatistics.class.getName(), criteria);
				statusType = "hourly";
			}
			
			this.banners = (List<BannerCampaigns>) CommonFunctionDelegator.getAllfromtable(BannerCampaigns.class.getName());
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	public List<BannerCampaigns> getBanners() {
		return banners;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<BannerSettings> getBannerType() {
		return bannerType;
	}

	public Integer getBannerTypeId() {
		return bannerTypeId;
	}

	public void setBannerTypeId(Integer bannerTypeId) {
		this.bannerTypeId = bannerTypeId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

//	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
}
